package com.example.administrator.myweiyue.ui.activity;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.example.administrator.myweiyue.R;
import com.example.administrator.myweiyue.log.LogUtils;
import com.example.administrator.myweiyue.ui.base.BaseActivity;
import com.example.administrator.myweiyue.ui.component.AppComponent;

import org.reactivestreams.Subscription;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Flowable;
import io.reactivex.FlowableSubscriber;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import pl.droidsonroids.gif.GifImageView;

/**
 * Created by Administrator on 2018-01-24.
 */

public class WelComeActivity extends BaseActivity {
    @BindView(R.id.gifImageView)
    GifImageView mGifImageView;

    @BindView(R.id.tv_skip)
    TextView mTvSkip;
    private Subscription mSubscription;


    @Override
    public int getLayoutId() {
        return R.layout.welcome_layout;
    }

    @Override
    public void initInjector(AppComponent appComponent) {

    }

    @Override
    public void afterViewCreate(View mRootView) {
        Flowable.interval(1, TimeUnit.SECONDS)
                .doOnNext(new Consumer<Long>() {
                    @Override
                    public void accept(@NonNull Long aLong) throws Exception {
//                        aLong默认为0，每间隔设置的时间就自增

                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new FlowableSubscriber<Long>() {
                    @Override
                    public void onSubscribe(@NonNull Subscription s) {
                        mSubscription = s;
                        s.request(Long.MAX_VALUE);
                    }
                    @Override
                    public void onNext(Long aLong) {
                        if (aLong >= 4) {
                            mSubscription.cancel();
                            startActivity(new Intent(context, MainActivity.class));
                        }
                        mTvSkip.setText("跳过"+(4-aLong));
                        LogUtils.debug("  " + aLong);
                    }

                    @Override
                    public void onError(Throwable t) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
//        Observable.
//                just(0).
//                doOnNext(integer -> LogUtils.debug("子线程"))
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(integer -> LogUtils.debug("主线程"));


//        Observable.create(e -> {
////            LogUtils.debug("1-----"+Thread.currentThread().getId());
//           //子线程
//            e.onNext(0);
//            e.onNext(1);
//
//
//        })
//          .subscribeOn(Schedulers.io())
//          .observeOn(AndroidSchedulers.mainThread())
//          .doOnNext(new Consumer<Object>() {
//              @Override
//              public void accept(@NonNull Object o) throws Exception {
//                  LogUtils.debug("2-----"+o);
//              }
//          }).observeOn(Schedulers.io())
//                .doOnNext(new Consumer<Object>() {
//                    @Override
//                    public void accept(@NonNull Object o) throws Exception {
//                        LogUtils.debug("3-----"+o);
//
//                    }
//                }).observeOn(AndroidSchedulers.mainThread()).doOnNext(new Consumer<Object>() {
//            @Override
//            public void accept(@NonNull Object o) throws Exception {
//                LogUtils.debug("4-----"+o);
//            }
//        })
//          .subscribe(new Consumer<Object>() {
//              @Override
//              public void accept(@io.reactivex.annotations.NonNull Object o) throws Exception {
//                  Toast.makeText(WelComeActivity.this, "nihao", Toast.LENGTH_SHORT).show();
//              }
//          }

//          );

//        Observable.create(e -> {
//            e.onNext(0);
//            //第一个input类型，第二个output类型
//        }).map((Function<Object, Object>) o -> {
//            int i=(Integer) o+521;
//            return i;
//        })
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(o -> {
//                    Toast.makeText(WelComeActivity.this, "nihao", Toast.LENGTH_SHORT).show();
//                });
//
    }

    @Override
    public void initData() {

    }

    @Override
    public void onRetry() {

    }


    @OnClick(R.id.tv_skip)
    public void onViewClicked() {
        startActivity(new Intent(this, MainActivity.class));
    }

    @Override
    protected void onDestroy() {
        if (mSubscription != null) {
            mSubscription.cancel();
        }
        super.onDestroy();
    }


}
